package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: CourseServiceImpl
 * Package: com.qk.service.impl
 * Description:
 *
 * @Author zt
 * @Create 2025/7/28 23:43
 * @Version 1.0
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    /*
    1.课程列表查询
     */
    @Override
    public PageResult<Course> listCourses(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);

        //调用mapper接口查询
        List<Course> courses =courseMapper.selectByCondition(name, subject, target);

        //构建分页查询之后的结果
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /*
    2.删除课程
     */

    @Override
    public void delete(Integer id) {
        courseMapper.delete(id);
    }
    /*
    3.添加课程
     */

    @Override
    public void add(Course course) {
        //补全基础属性
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.add(course);
    }

    @Override
    public Course getById(Integer id) {
        return courseMapper.getById(id);
    }

    @Override
    public void update(Course course) {
        //补全属性：更新时间
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public List<Course> list() {
        return courseMapper.list();
    }

    @Override
    public List<Course> listBySubject(Integer subject) {
        return courseMapper.listBySubject(subject);
    }
}

